package org.mespilus.mespilusweb.ui.usermanagement.client;

import java.util.ArrayList;
import java.util.List;

import org.mespilus.mespilusweb.ui.usermanagement.client.presenter.UserDetailsPresenter;

import com.google.gwt.user.client.ui.HorizontalPanel;
import com.gwtext.client.core.Function;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.PaddedPanel;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.form.FieldSet;
import com.gwtext.client.widgets.form.TextField;

public class UserDetailsPanel {
	protected UserDetailsPresenter presenter;
	protected FieldSet fieldSet = new FieldSet();
	protected TextField mailField = new TextField("Mail", "mail", 120);
	protected TextField nameField = new TextField("Name", "name", 120);
	protected TextField passwdField = new TextField("Password", "passwd", 120);
	protected TextField firstNameField = new TextField("Surname1", "firstName", 120);
	protected TextField secondNameField = new TextField("Surname2", "secondName", 120);
	protected TextField NIFField = new TextField("NIF", "NIF", 120);
	protected TextField streetField = new TextField("Street", "street", 120);
	protected TextField postalCodeField = new TextField("Postal code", "postalCode", 120);
	protected TextField cityField = new TextField("City", "city", 120);
	protected TextField countryField = new TextField("Country", "country", 120);
	protected TextField phone1Field = new TextField("Phone1", "phone1", 120);
	protected TextField phone2Field = new TextField("Phone2", "phone2", 120);
	protected TextField basketField = new TextField("Basket", "basket", 120);
	
	
	public UserDetailsPanel() {
	}
	
	public UserDetailsPresenter getPresenter() {
		return presenter;
	}

	public void setPresenter(UserDetailsPresenter presenter) {
		this.presenter = presenter;
	}

	public Panel getUseDataPanel(){
		fieldSet.setLabelWidth(90);
		fieldSet.setTitle("User Details");
		fieldSet.setAutoHeight(true);  
		fieldSet.setBorder(false);
		
		fieldSet.add(mailField);
		fieldSet.add(nameField);
		fieldSet.add(passwdField);
		fieldSet.add(firstNameField);
		fieldSet.add(secondNameField);
		fieldSet.add(NIFField);
		fieldSet.add(streetField);
		fieldSet.add(postalCodeField);
		fieldSet.add(cityField);
		fieldSet.add(countryField);
		fieldSet.add(phone1Field);
		fieldSet.add(phone2Field);
		fieldSet.add(basketField);

		HorizontalPanel horizontalPanelButtons = new HorizontalPanel();
		Button saveButton = new Button("Save");
		Button clearButton = new Button("Clear");
		clearButton.addListener("click", new Function(){
			@Override
			public void execute() {
				clearForm();
			}
		});
		saveButton.addListener("click", new Function(){
			@Override
			public void execute() {
				List<String> data = new ArrayList<String>();
				data.add(mailField.getValueAsString());
				data.add(nameField.getValueAsString());
				data.add(firstNameField.getValueAsString());
				data.add(secondNameField.getValueAsString());
				data.add(NIFField.getValueAsString());
				data.add(streetField.getValueAsString());
				data.add(postalCodeField.getValueAsString());
				data.add(cityField.getValueAsString());
				data.add(countryField.getValueAsString());
				data.add(phone1Field.getValueAsString());
				data.add(phone2Field.getValueAsString());
				presenter.saveUser(data);
			}
		});
		horizontalPanelButtons.add(clearButton);
		horizontalPanelButtons.add(saveButton);
		fieldSet.add(horizontalPanelButtons);
		PaddedPanel paddedPanel = new PaddedPanel(fieldSet, 0, 10, 0, 0);
		paddedPanel.setTitle("Details");
		paddedPanel.setFrame(true);
		paddedPanel.setWidth(350);
		paddedPanel.setHeight(450);
		paddedPanel.setPaddings(5);
		return paddedPanel;
	}

	public void clearForm() {
		mailField.setValue("");
		nameField.setValue("");
		passwdField.setValue("");
		firstNameField.setValue("");
		secondNameField.setValue("");
		NIFField.setValue("");
		streetField.setValue("");
		postalCodeField.setValue("");
		cityField.setValue("");
		countryField.setValue("");
		phone1Field.setValue("");
		phone2Field.setValue("");
		basketField.setValue("");
	}
	
	public void fillValues(List<String> person){
		mailField.setValue(person.get(0));
		//mustChangePasswd;
		nameField.setValue(person.get(1));
		firstNameField.setValue(person.get(2));
		secondNameField.setValue(person.get(3));
		NIFField.setValue(person.get(4));
		streetField.setValue(person.get(5));
		postalCodeField.setValue(person.get(6));;
		cityField.setValue(person.get(7));
		countryField.setValue(person.get(8));
		phone1Field.setValue(person.get(9));
		phone2Field.setValue(person.get(10));
	}
}
